package com.misty.admin.controller.excelController;

import com.misty.admin.service.BlogService;
import com.misty.api.param.file.FileParam;
import com.misty.api.service.ExcelService;
import com.misty.common.bean.Result;
import com.misty.dao.dto.BlogDto;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName : ExcelController
 * @Description :
 * @Author :
 * @Date: 2022-12-23 22:13
 */
@Controller
@Slf4j
@RequestMapping("/excel")
public class ExcelController {

    @Autowired
    private ExcelService excelService;

    @Autowired
    private BlogService blogService;

    @RequestMapping(value = "/uploadBlog", method = RequestMethod.POST)
    @ApiOperation(value = "上传博客excel")
    @ResponseBody
    // 处理医疗项目excel
    public Result uploadBlog(FileParam fileParam) throws IOException {
        log.info("==============开始上传博客excel================");
//        String key = UUID.randomUUID().toString();
        String suffix = fileParam.getSuffix();
        MultipartFile file = fileParam.getFile();
        if (!"xlsx".equals(suffix) && !"xls".equals(suffix)) {
            return Result.error("导入失败，请选择正确的文件格式支持xlsx或xls");
        }

        Boolean clock = blogService.uploadExcel(fileParam);
        if (clock) {
            return Result.success("上传excel成功！");
        } else {
            return Result.error( "上传excel失败！");
        }
    }


    @GetMapping("/downloadBlog")
    @ApiOperation(value = "DownloadBlog")
    public void download(@RequestParam("fileType") String fileType,
                         HttpServletRequest request, HttpServletResponse response) {
        Result<List<BlogDto>> allInfo = blogService.getAllInfo();
        if (allInfo.isErrorResult()) {
            return ;
        }
        List<BlogDto> data = allInfo.getData();
        excelService.download(fileType,data,request,response);
    }

    @GetMapping("/downloadTemplate")
    public void downloadTemplate(@RequestParam("fileType") String fileType,
                                 HttpServletRequest request, HttpServletResponse response) {
        excelService.downloadTemplate(fileType, request, response);

    }
}
